FlowEditor.prototype.NAME_SPACE = "http://www.w3.org/2000/svg";
function FlowEditor(svgDoc){
	this.svgDoc = svgDoc;
	
	this.init = function(){
		//create defs
		createDefs(this.svgDoc);	
		//create bg rect
		createBgRect(this.svgDoc);
		//create canvas svg
		createCanvas(this.svgDoc);	
		//create toolBox svg
		createToolBox(this.svgDoc);
	}
	
	this.leftButtonDown = function(evt){
		alert(evt.target.getAttribute("id"));
	}
	
	this.middleButtonDown = function(evt){
		alert(this.getSvgXml());
	}
	
	
	this.getElement = function(id){
		return this.svgDoc.getElementById(id);
	}
	
	this.getSvgXml = function(){
		return printNode(this.svgDoc);
	}
	
	
	/**
	 * private function
	 * @param {Object} svgDoc
	 */
	function createDefs(svgDoc){
		var defs = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"defs");
		var linearGradient = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"linearGradient");
		linearGradient.setAttribute("id","bgColor");
		linearGradient.setAttribute("x1","0%");
		linearGradient.setAttribute("y1","0%");
		linearGradient.setAttribute("x2","0%");
		linearGradient.setAttribute("y2","100%");
		var stop1 = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"stop");
		stop1.setAttribute("offset","0%");
		stop1.setAttribute("style","stop-color:rgb(193,204,229);stop-opacity:1");
		var stop2 = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"stop");
		stop2.setAttribute("offset","100%");
		stop2.setAttribute("style","stop-color:rgb(221,247,241);stop-opacity:1");
		linearGradient.appendChild(stop1);	
		linearGradient.appendChild(stop2);
		defs.appendChild(linearGradient);
		svgDoc.documentElement.appendChild(defs);
	}
	
	function createBgRect(svgDoc){
		var width = svgDoc.documentElement.getAttribute("width");
		var height = svgDoc.documentElement.getAttribute("height");
		var bgRect = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"rect");
		bgRect.setAttribute("id","bgRect");
		bgRect.setAttribute("x","0");
		bgRect.setAttribute("y","0");
		bgRect.setAttribute("width",width);
		bgRect.setAttribute("height",height);
		bgRect.setAttribute("fill","url(#bgColor)");
		svgDoc.documentElement.appendChild(bgRect);
	}
	
	function createCanvas(svgDoc){
		var width = svgDoc.documentElement.getAttribute("width");
		var height = svgDoc.documentElement.getAttribute("height");
		var canvasSvg = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"svg");
		canvasSvg.setAttribute("id","canvasSvg");
		canvasSvg.setAttribute("width",width);
		canvasSvg.setAttribute("height",height);
		canvasSvg.setAttribute("viewBox","0 0 "+width+" "+height);
		svgDoc.documentElement.appendChild(canvasSvg);
	}
	
	function createToolBox(svgDoc){
		var toolBoxSvg = svgDoc.createElementNS(FlowEditor.prototype.NAME_SPACE,"svg");
		toolBoxSvg.setAttribute("id","toolBoxSvg");
		toolBoxSvg.setAttribute("width",161);
		toolBoxSvg.setAttribute("height",31);
		svgDoc.documentElement.appendChild(toolBoxSvg);
	}
}